package com.d0319;

public class EggTest1 {
    Object flag=new Object();
    volatile boolean hasEggs=false;
    Thread human =new Thread(()->{
        while (true){
            if (!hasEggs){
                synchronized (flag){

                    try {
                        System.out.println(Thread.currentThread().getName()+"等");
                        flag.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }else {
                System.out.println(Thread.currentThread().getName()+"收");
                hasEggs=false;
            }
        }
    });
    Thread human1 =new Thread(()->{
        while (true){
            if (!hasEggs){
                synchronized (flag){

                    try {
                        System.out.println(Thread.currentThread().getName()+"等");
                        flag.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }else {
                System.out.println(Thread.currentThread().getName()+"收");
                hasEggs=false;
            }
        }
    });
    Thread hen=new Thread(()->{
       while (true){
           try {
               Thread.sleep(3000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           hasEggs=true;
           synchronized (flag){
               flag.notify();
           }
       }
    });

    public static void main(String[] args) {
        EggTest1 eggTest=new EggTest1();
        eggTest.human1.start();
        eggTest.human.start();
        eggTest.hen.start();
    }
}
